package models

import (
	"github.com/astaxie/beego/context"
	"github.com/astaxie/beego/orm"
	"time"
)

func (a *AdminLog) TableName() string {
	return "admin_log"
}

//AdminUser实体类
type AdminLog struct{
	Id          uint
	AdminUserId uint    `description:"管理员ID"`
	Username    string    `orm:"size(60)"description:"管理员名称"`
	Url         string     `orm:"size(1024)"description:"操作页面"`
	Title       string    `orm:"size(100)"description:"日志标题"`
	Content     string    `orm:"type(text)"description:"日志内容"`
	Ip          string    `orm:"size(100)"description:"ip地址"`
	Useragent   string     `orm:"size(512)" description:"User-Agent"`
	CreatedAt time.Time `orm:"auto_now_add;type(datetime)"description:"创建时间"`
}

//添加用户日志
func AdminLogAddLog(ctx *context.Context,title string){
	adminUserSession := ctx.Input.Session("admin_user")
	var adminUserId uint
	var userName string
	if adminUserSession != nil{
		adminUser := adminUserSession.(*AdminUser)
		adminUserId = adminUser.Id
		userName = adminUser.Username
	}
	adminLog := AdminLog{
		AdminUserId :adminUserId,
		Username :userName,
		Url : ctx.Request.RequestURI,
		Title :title, //请求标题
		Content :"", //请求数据
		Ip :ctx.Request.RemoteAddr,
		Useragent :ctx.Request.UserAgent(),
	}
	m := orm.NewOrm()
	_, _= m.Insert(&adminLog)
}